package com.ruoyi.file.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.file.domain.bo.FilesBo;
import com.ruoyi.file.domain.vo.FilesVo;
import com.ruoyi.file.domain.Files;
import com.ruoyi.file.mapper.FilesMapper;
import com.ruoyi.file.service.IFilesService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 上传的文件列表Service业务层处理
 *
 * @author ruoyi
 * @date 2023-03-12
 */
@RequiredArgsConstructor
@Service
public class FilesServiceImpl implements IFilesService {

    private final FilesMapper baseMapper;

    /**
     * 查询上传的文件列表
     */
    @Override
    public FilesVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    @Override
    public FilesVo getByMapperPath(String path) {
        LambdaQueryWrapper<Files> qw = Wrappers.lambdaQuery();
        qw.eq(Files::getMapperPath,path);
        return baseMapper.selectVoOne(qw);
    }

    /**
     * 查询上传的文件列表列表
     */
    @Override
    public TableDataInfo<FilesVo> queryPageList(FilesBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Files> lqw = buildQueryWrapper(bo);
        Page<FilesVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询上传的文件列表列表
     */
    @Override
    public List<FilesVo> queryList(FilesBo bo) {
        LambdaQueryWrapper<Files> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Files> buildQueryWrapper(FilesBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Files> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getRealPath()), Files::getRealPath, bo.getRealPath());
        lqw.eq(StringUtils.isNotBlank(bo.getMapperPath()), Files::getMapperPath, bo.getMapperPath());
        lqw.like(StringUtils.isNotBlank(bo.getFlieName()), Files::getFlieName, bo.getFlieName());
        lqw.eq(StringUtils.isNotBlank(bo.getFileType()), Files::getFileType, bo.getFileType());
        return lqw;
    }

    /**
     * 新增上传的文件列表
     */
    @Override
    public Boolean insertByBo(FilesBo bo) {
        Files add = BeanUtil.toBean(bo, Files.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改上传的文件列表
     */
    @Override
    public Boolean updateByBo(FilesBo bo) {
        Files update = BeanUtil.toBean(bo, Files.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Files entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除上传的文件列表
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean deletById(Long id) {
        return baseMapper.deleteById(id)>0;
    }
}
